package org.example.server.messagehandler.chat;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.example.common.codec.Invocation;
import org.example.common.dispacher.MessageHandler;
import org.example.server.message.chat.ChatRedirectToUserRequest;
import org.example.server.message.chat.ChatSendResponse;
import org.example.server.message.chat.ChatSendToAllRequest;
import org.example.server.server.NettyChannelManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @title
 * @time 2025/6/16 17:32
 */
@Component
@Slf4j
public class ChatSendToAllHandler  implements MessageHandler<ChatSendToAllRequest> {
    @Autowired
    private NettyChannelManager nettyChannelManager;
    /**
     * 处理消息
     * @param ch
     * @param message
     */
    @Override
    public void execute(Channel ch, ChatSendToAllRequest message) {
        // 响应给发信人
        ChatSendResponse response = new ChatSendResponse(message.getMsgId(), 200, "发送成功");
        ch.writeAndFlush(new Invocation(ChatSendResponse.TYPE, response));
        // 创建转发消息题，广播发送
        // 将信息转发给收信人
        ChatRedirectToUserRequest redirect = new ChatRedirectToUserRequest(message.getMsgId(),
                message.getContent());
        nettyChannelManager.sendAll(new Invocation(ChatRedirectToUserRequest.TYPE, redirect));
    }

    /**
     * 获取消息类型
     * @return
     */
    @Override
    public String getType() {
        return ChatSendToAllRequest.TYPE;
    }
}
